// pages/search/search.js
//引入封装好的request
const http = require("../../utils/http");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: [{
        id: "1",
        name: "洲"
      },
      {
        id: "2",
        name: "季节"
      },
    ],
    zhouList: [{
        id: "1",
        "name": "欧洲"
      },
      {
        id: "2",
        "name": "亚洲"
      },
      {
        id: "3",
        "name": "大洋洲"
      },
      {
        id: "4",
        "name": "非洲"
      },
      {
        id: "5",
        "name": "北美洲"
      },
      {
        id: "6",
        "name": "南美洲"
      },
    ],
    jijieList: [{
      id: "1",
      "name": "春意阑珊"
    }, {
      id: "2",
      "name": "盛夏果实"
    }, {
      id: "3",
      "name": "秋来秋去"
    }, {
      id: "4",
      "name": "冬之恋曲"
    }],
    list: [],
    areaName: '欧洲',
    titleName: '洲',
    europe: [],
    jijieArr: [],
    // 搜索关键字
    searchKey: "" //搜索关键字
  },
  //跳转到列表页
  goList() {
    wx.navigateTo({
      url: '/pages/list/list?searchKey=' + this.data.searchKey,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      list: this.data.zhouList
    })
    this.searchList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  titleList(ev) {
    if (ev) {
      let list, areaName;
      if (ev.currentTarget.dataset.name === '洲') {
        areaName = this.data.zhouList[0].name
        list = this.data.zhouList
      } else {
        areaName = this.data.jijieList[0].name
        list = this.data.jijieList
      }
      this.setData({
        areaName,
        list,
        titleName: ev.currentTarget.dataset.name,
      })
      this.searchList()
    }
  },

  searchList(ev) {
    if (ev) {
      this.setData({
        areaName: ev.currentTarget.dataset.name
      })
    }
    if (this.data.titleName === '洲') {
      http.request(`requirement/destination/countries/${encodeURIComponent(this.data.areaName)}`, "", "get").then(res => {
        this.setData({
          europe: res.data
        })
        console.log(res.data);
      })
    } else {
      http.request(`requirement/request/getMatchedProducts/`, {
        product_type: 1,
        request_season: [this.data.areaName]
      }, "post").then(res => {
        this.setData({
          jijieArr: res.data.matchedProducts
        })
      })
    }

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})